<?php
/**
 * 订单管理
 * @author tangbl
 *
 */
class OrderAction extends CommonAction {
 
	/**
	 *  定义数据表
	 */
	
	private $order       = "Order";//定义订单数据表
	private $OrderDetail = "OrderDetail";//定义订单数据表
	
	/**
	 *  定义提示信息
	 */
	
	private $order_error_msg = "下单失败";//定义订单数据表
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 付款
     * @access public
     * @param  String $orderno 订单编号
     * return  array
     */
    public function payment(){
    	
    	$map['orderno'] = $this->_req("orderno","trim","");
    	
    	$model = D("Order");
    	$row   = $model->where($map)->find();
    	
    	if($row['id']){
    		$this->success($row);
    	}else{
    		$this->error("订单号不存在");
    	}
    }
    
    /**
     * 确认订单
     */
    public function confirm(){
    	$val = $this->_req("val","trim","");
    	 
    	if($val){
    	
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    	
    			$map['id']       = $v;
    			$map['status']   = 2;
    			$map['time_que'] = time();
    			$data = D('Order')->save($map);
    		}
    		$this->success($data);
    	}else{
    		$this->error('请选择需要确认的订单');
    	}
    }
    
    /**
     * 根据订单id获取该订单的备注列表
     */
    public function orderblist(){
    	
    	$id    = $this->_req("id");
    	$model = M("OrderBak");
    	
    	$data  = $model->where(array("did"=>$id))->select();
    	$this->success($data);
    }
    
    /**
     * 添加订单备注
     */
    public function orderbak(){
    	$id            = $this->_req("id");
    	$map['price']  = $this->_req("price");
    	$map['opera']  = $this->_req("opera");
    	$map['remark'] = $this->_req("remark");
    	
    	$model = D("Order");
    	$view  = $model->find($id);
    	
    	if($view['id']){
    		if($map['price']){
	    		if($map['opera']=="plus"){
	    			$cat['b_price'] = $view['b_price']+$map['price'];
	    		}else{
	    			$cat['b_price'] = $view['b_price']-$map['price'];
	    		}
	    		$cat['id'] = $view['id'];
	    		$data = $model->save($cat);
    		}
    		$map['addtime'] = time();
    		$map['did']     = $view['id'];
    		$map['addid']   = $this->_req("addid");
    			 
    		$query = M("OrderBak")->add($map);
    		if($query){
    			$this->success("订单备注成功");
    		}else{
    			$this->error("订单备注失败");
    		}
    	}else{
    		$this->error("订单备注失败");
    	}
    }
    
    /**
     * 根据订单id，获取订单备注
     */
    public function orderbakList(){
    	
    	$id = $this->_req("id","intval",0);
    	
    	if($id){
    		$map['did'] = $id;
    		$data = M("OrderBak")->where($map)->select();
    		
    		if($data){
    			$this->success($data);
    		}else{
    			$this->error();
    		}
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 付款结账
     * @access public
     * @param  array  $map     应用参数(company,outletid,addid,addno)
     * @param  String $oldid   订单id
     * @param  array  $payment 支付参数array("Cash"=>1,"Bank"=>2,"Other"=>3)
     * @return array
     */
    public function pay(){
    	
    	$map['company'] = $this->_req("company", "intval");
    	$map['id']      = $this->_req("oldid"  , "intval");
    	
    	$model = D("Order");
    	$order = $model->where($map)->find();
    	
    	$where = array("company"=>$map['company'],"orderno"=>$order['orderno']);
    	$pay   = M("OrderPay")->where($where)->find();
    	
    	$oldid = $order['id'];
    	$payid = $pay['id'];
    	
    	if($oldid && $payid){
    		
    		$total = $pay['totalfee'];
    		$rent  = $pay['rent'];
    		$pays  = $this->_req("payment");
    		$data  = parent::getParams();
    		
    		$data['addtime'] = time();
    		$data['payid']   = $pay['id'];
    		$module = M("OrderPayLog");
    		 
    		$fee = 0 ;
    		foreach($pays AS $k=>$v){
    		
    			$payfee = doubleval($v);
    			if( $payfee >0 ){
    				$data['payment'] = $k;
    				$data['payfee']  = $v;
    				$module->add($data);
    			}
    			$fee += $payfee;
    		}
    		
    		$backfee = $payfee-$total;
    		$payment = array_search(max($pays), $pays);
    		 
    		$save['ispay']   = 1;
    		$save['payment'] = $payment;
    		$id1 = $model->where("id=".$oldid)->save($save);
    		 
    		$save['backfee'] = $backfee;
    		$id2 = M("OrderPay")->where("id=".$payid)->save($save);
    		
    		if($id1 && $id2){
    			$this->error($id1+$id2);
    		}else{
    			$this->error();
    		}
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 根据对应的条件查询订单详情
     * @access public
     * @param  int    $id  用户id
     * @return array
     */
    public function detail(){
    
    	$id      = $this->_get("id","intval",0);
    	$orderno = $this->_get("orderno","trim","");
    	 
    	if($id){
    		$map["id"] = $id;
    	}
    	if($orderno){
    		$map["orderno"] = $orderno;
    	}
    
    	$model = D("Order");
    	$data  = $model->where($map)->find();
    
    	$this->success($data);
    }
    
    /**
     * 根据订单编号获取订单详情列表
     */
    public function dlist(){
    	
    	$map['orderno'] = $this->_req("orderno","trim","");
    	
    	$view = D("DetailProductView")->where($map)->order('id desc')->select();
    	$this->success($view);
    }
  
    /**
     * 分配订单到门店
     * @access public
     * @param  array  $map      应用参数(company,outletid,addid,addno)
     * 
     * @param  int    $oldid    订单id
     * @param  int    $outlet   分配门店id
     * @param  String $content  操作说明
     * @return array
     */
    public function allotOutlet(){
   		
    	$map = parent::getParams();
    	
    	$map['oldid']    = $this->_req("oldid"  ,"intval");
    	$map['outlet']   = $this->_req("outlet" ,"intval");
    	$map['content']  = $this->_req("content","trim","");
    	
    	$model = D("Order");
    	$id    = $model->allotOutlet($map);
    	if($id){
    		$this->success($id);
    	}else{
    		$this->error($id);
    	}
    }
    
    /**
     * 分配订单到用户
     * @access public
     * @param  array  $map      应用参数(company,outletid,addid,addno)
     * 
     * @param  int    $oldid    订单id  required
     * @param  int    $userid   旧用户id 
     * @param  int    $adminid  新用户id required
     * @param  String $content  操作说明
     * @return array
     */
    public function allotUser(){
    	 
    	$map = parent::getParams();
    	
    	$map['oldid']    = $this->_req("oldid"  ,"intval");
    	$map['adminid']  = $this->_req("adminid","intval");
    	$map['userid']   = $this->_req("userid" ,"intval");
    	$map['content']  = $this->_req("content","trim","");
    	
    	$model = D("Order");
    	$id    = $model->allotUser($map);
    	if($id){
    		$this->success($id);
    	}else{
    		$this->error($id);
    	}
    }
    
    /**
     * 订单列表分页
     * @access public
     * @param  array  $map  应用参数(company,outletid,addid,addno)
     * @return array
     */
    public function page(){
    	
    	$cat['city_id']   = $this->_req("city_id","intval",0);
    	$cat['addid']     = $this->_req("addid","intval",0);
    	$cat['time_view'] = $this->_req("time_view","intval",0);
    	
    	$model = D("Order");
    	$map   = $model->getRoleOrder($cat);
    	$where = $this->_req("where");
    	
    	if($where['keywords']){
    		$map['orderno|username|mobile'] = array("like","%".$where['keywords']."%");
    	}
    	
    	if($where['status']){
    		$map['status'] = $where['status'];
    	}
    	
    	if($where['dealid']){
    		$map['dealid'] = $where['dealid'];
    	}
    	
    	if($where['city']){
			if($where['city'] == 850){
    			$map['city'] = array('like',array('850','930'),'or');
			}else{
    			$map['city'] = $where['city'];
			}
    	}
    	
    	if($where['outletid']){
    		$map['outletid'] = $where['outletid'];
    	}
    	
    	if($where['start']&&$where['end']){
    		$map['addtime'] = array(array('egt',$where['start']),array('elt',$where['end'])); 
    	}else{
    		if($where['start']){
    			$map['addtime'] = array('egt',$where['start']);
    		}
    		if($where['end']){
    			$map['addtime'] = array('elt',$where['end']);
    		}
    	}
    	
    	if($cat['city_id']){
			if($cat['city_id'] == 850){
    			$map['city']  = array('like',array('850','930'),'OR');
			}else{
    			$map['city']  = $cat['city_id'];
			}
    		/*$map['city']  = $cat['city_id'];*/
    	}
		
	    $map['isdel'] = 2;
    	
    	$desc    = $this->_get("orderDirection","trim","desc");;
		$order   = $this->_get("orderName","trim","addtime");
		$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage = $this->_get("p","intval",1);
    	
    	$count = $model->where($map)->count();
    	$list  = $model->where($map)->order($order.' '.$desc)
    			 ->page($nowPage.','.$pageNum)->select();
		Log::write('orderpage_sql:'.$model->getLastSql(),Log::DEBUG);
    	
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    /**
     * 订单列表
     * @access public
     * @param  array  $map  应用参数(company,outletid,addid,addno)      
     * @return array
     */
    public function index(){
    
    	$config  = parent::getDataAccess();
    	$table   = $config["table"];
    	$map     = $config["map"];
    	
    	$model   = D($table);
    	$list    = $model->where($map)->select();
    	 
    	$this->success($list);
    }
    
    /**
     * 无分页订单列表
     */
    public function view(){
    	
    	$start      = $this->_req("start","trim","");
    	$end        = $this->_req("end",  "trim","");
    	$dealid     = $this->_req("dealid","intval",0);
    	$dataid     = $this->_req("dataid","intval",0);
    	$city       = $this->_req("city","intval",0);
    	$keywords   = $this->_req("keywords","trim",'');
    	$status     = $this->_req("status","intval",0);
    	$outletid   = $this->_req("outletid","intval",0);
		$orderby    = $this->_req('orderby','trim','addtime desc');
    	
    	if($start&&$end){
    		$map['addtime'] = array(array("egt",$start),array("elt",$end));
    	}elseif($start){
    		$map['addtime'] = array("egt",$start);
    	}elseif($end){
    		$map['addtime'] = array("elt",$end);
    	}
    	if($dealid){
    		$map['dealid'] = $dealid;
    	}
    	if($dataid){
    		$map['dataid'] = $dataid;
    	}
    	if($city){
    		$map['city'] = $city;
    	}
    	if($status){
    		$map['status'] = $status;
    	}
    	if($outletid){
    		$map['outletid'] = $outletid;
    	}
    	if($keywords){
    		$map['orderno|username|mobile'] = array("like","%".$keywords."%");
    	}
    	
    	$map['isdel'] = 2;
    	
    	$model = D("Order");
    	$this->success($model->where($map)->order($orderby)->select());
    }
    
    /**
     * 添加订单
     */
    public function add(){
    	
    	$param = array(
    		"after"  => "orderdetail_add_after",
    		"return" => true
    	);
    	$data = parent::do_add("Order",$param);
    	$this->success($data);
    }
    /**
     * 根据订单id查询订单
     */
    public function find(){
    	$id = $this->_req("id");
    	$this->success(M($this->order)->find($id));
    }
    
    /**
     * 修改订单
     */
    public function edit(){
    	 
    	$param = array(
    			"before"  => "orderdetail_edit_before",
    			"return" => true
    	);
    	parent::do_edit("Order",$param);
    }
    
    /**
     * 根据用户编号查询订单
     */
    public function order_select(){
    	 
    	$map['dataid'] = $this->_req("dataid");
    	$data = M($this->order)->where($map)->order('addtime desc')->select();
    	$this->success($data);
    }
    /**
     * 根据订单编号查询订单详情
     * @access public
     * @param  char   $orderno
     * @return array
     */
    public function byorderno(){
    
    	$map['orderno'] = $this->_req("orderno");
    	$data  = M($this->OrderDetail)->where($map)->select();
    
    	$this->success($data);
    }
    /**
     * 根据订单编号订单
     * @access public
     * @param  char   $orderno
     * @return array
     */
    public function orderbyno(){
    
    	$map['orderno'] = $this->_req("orderno");
    	$data  = M($this->order)->where($map)->find();
    
    	$this->success($data);
    }
   
    /**
     * 删除订单
     */
    public function del(){
    	
    	$val = $this->_req("val");
    	
    	if($val){
    		
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    			 
    			$map['id']    = $v;
    			$map['isdel'] = 1;
    			$data = D('Order')->save($map);
    		}
    		$this->success($data);
    	}else{
    		$this->error('请选择需要删除的订单');
    	}
    }
    
    /**
     * 取消订单
     */
    public function cancel(){
    	 
    	$val = $this->_req("val");
    	 
    	if($val){
    
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    
    			$map['id']     = $v;
    			$map['status'] = -1;
    			$data = D('Order')->save($map);
    		}
    		$this->success($data);
    	}else{
    		$this->error('请选择需要取消的订单');
    	}
    }
    
    /**
     * 分配订单
     */
    public function deal(){
    	
    	$id     = $this->_req("id","intval",0);
    	$dealid = $this->_req("dealid","intval",0);
    	$model  = D('Order');
    	 
    	if($id){
    		$map['id']      = $id;
    		$map['status']  = 5;
    		$map['dealid']  = $dealid;
    		$map['time_fen']= time();
    		$data = $model->save($map);
    		
    		$this->success($data);
    	}else{
    		$this->error('请选择需要分配的订单');
    	}
    }
    
    /**
     * 查询用户最后下的一笔单
     */
    public function getLastOrder(){
    	 
    	$id = $this->_req("id");
    	$detail = M("Order")->find($id);
    	$this->success($detail);
    }
    
    /**
     * 删除某订单中某个商品
     */
    public function delProduct(){
    	
    	$id     = $this->_req("id","intval",0);
    	$oid    = $this->_req("oid","intval",0);
    	$model  = M("OrderDetail"); 
    	$module = D("Order");
    	
    	if($id&&$oid){
    		
    		$view = $model->find($id);
    		$data = $module->find($oid);
    		
    		if($view['flag']=='drink'){
    			$map['drinkfee']  = $data['drinkfee']-$view['total'];
    		}else{
    			$map['dishesfee'] = $data['dishesfee']-$view['total'];
    		}
    		
    		$map['id']         = $oid;
    		$map['orderfee']   = $data['orderfee']   - $view['total'];   //订单总金额
    		$map['settlefee']  = $data['settlefee']  - $view['mprice'];  //结算金额（成本价总金额）
    		$map['receivefee'] = $data['receivefee'] - $view['mprice'];  //实收金额（总金额-结算金额）
    		$module->save($map);
    		$model->delete($id);
    		
    		$this->success("订单修改成功");
    	}else{
    		$this->error('删除订单商品失败');
    	}
    }
    
    
   //删除订单备注
    public function dealbak(){
    	
    	$ordid  = $this->_req('ordid', 'intval',0);
    	$dataid = $this->_req('dataid','intval',0);
    	
    	$model  = M('Order');
    	$module = M('OrderBak');
    	$where  = array('id'=>$ordid);
    	$bak    = $module->find($dataid);
    	
    	$flag = true;
    	if($ordid && $dataid && $bak['id']){
    		if($bak['price'] && $bak['price']!='0.00' && $bak['opera']=='plus'){
    			$data = $model->where($where)->setDec('b_price',$bak['price']);
    			if(!$data){$flag = false;}
    		}
    		if($bak['price'] && $bak['price']!='0.00' && $bak['opera']=='minus'){
    			$data = $model->where($where)->setInc('b_price',$bak['price']);
    			if(!$data){$flag = false;}
    		}
    		
    		if($flag){
    			$result = $module->where(array('id'=>$dataid))->delete();
    			if($result){
    				$this->success($result);
    			}else{
    				$this->error('订单备注删除失败，请稍后重试');
    			}
    		}else{
    			$this->error('非法操作');
    		}
    	}else{
    		$this->error('非法操作');
    	}
    } 
    
    
}